package com.zcf.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zcf.common.json.Body;
import com.zcf.mapper.GameMapper;
import com.zcf.mapper.RechargeRecordMapper;
import com.zcf.pojo.Game;
import com.zcf.pojo.RechargeRecord;
import com.zcf.vo.out.LayuiJson;

@Service
public class GameService {

	@Autowired
	GameMapper gameMapper;
	@Autowired
	RechargeRecordMapper rechargeRecordMapper;

	/**
	 * 牛牛后台获取游戏管理
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public LayuiJson getgame(int pageNum, int pageSize) {
		LayuiJson lj = new LayuiJson();
		EntityWrapper<Game> w = new EntityWrapper<Game>();
		List<String> list1 = new ArrayList<String>();
		List list = gameMapper.selectPage(new Page<>(pageNum, pageSize), w.le("id", 3));
		Integer num = gameMapper.selectCount(w);
		if (list != null) {
			lj.setCode(0);
			lj.setCount(num);
			lj.setData(list);
			lj.setMsg("成功");
			return lj;
		}
		lj.setCode(1);
		lj.setCount(num);
		lj.setData(null);
		lj.setMsg("暂无数据");
		return lj;
	}

	/**
	 * 扎金花后台获取游戏管理
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public LayuiJson getZgame(int pageNum, int pageSize) {
		LayuiJson lj = new LayuiJson();
		EntityWrapper<Game> w = new EntityWrapper<Game>();
		List<String> list1 = new ArrayList<String>();
		List list = gameMapper.selectPage(new Page<>(pageNum, pageSize), w.ge("id", 4));
		Integer num = gameMapper.selectCount(w);
		if (list != null) {
			lj.setCode(0);
			lj.setCount(num);
			lj.setData(list);
			lj.setMsg("成功");
			return lj;
		}
		lj.setCode(1);
		lj.setCount(num);
		lj.setData(null);
		lj.setMsg("暂无数据");
		return lj;
	}

	/**
	 * 修改局数、消耗房卡数
	 * 
	 * @param game
	 * @return
	 */
	public Body updategame(Game game) {
		if (game.getGame_num() != null || game.getPay_num() != null) {
			boolean b = game.updateById();
			if (b) {

				return Body.BODY_200;
			}
		}

		return Body.BODY_400;
	}

	/**
	 * 牛牛获取房卡
	 */
	public Body getroomcard(Game game) {
		Map<String, Object> map;
		List<Game> list = game.selectList(new EntityWrapper<Game>().le("id", 3));
		List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < list.size(); i++) {
			map = new HashMap<String, Object>();
			map.put("game_num", list.get(i).getGame_num());
			map.put("pay_num", list.get(i).getPay_num());
			lists.add(map);
		}

		if (lists != null) {
			return Body.newInstance(lists);
		}
		return Body.BODY_401;
	}

	/**
	 * 扎金花获取房卡
	 */
	public Body getZroomcard(Game game) {
		Map<String, Object> map;
		List<Game> list = game.selectList(new EntityWrapper<Game>().ge("id", 4));
		List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < list.size(); i++) {
			map = new HashMap<String, Object>();
			map.put("game_num", list.get(i).getGame_num());
			map.put("pay_num", list.get(i).getPay_num());
			lists.add(map);
		}

		if (lists != null) {
			return Body.newInstance(lists);
		}
		return Body.BODY_401;
	}

	/**
	 * 查询订单信息
	 */
	public RechargeRecord rShow(String p2_ordernumber) {
		// Wrapper w = new EntityWrapper();
		// w.eq("p2_ordernumber", p2_ordernumber);
		RechargeRecord r = new RechargeRecord(p2_ordernumber);
		return rechargeRecordMapper.selectOne(r);
	}

	/**
	 * 添加充值订单
	 */
	public int addRecord_Play(String p2_ordernumber, String userid, String p7_productcode, String money,
			String p3_money) {
		SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
		sdf.applyPattern("yyyy-MM-dd HH:mm:ss");// a为am/pm的标记
		Date date = new Date();// 获取当前时间
		String createtime = sdf.format(date).toString();
		int start = 0;// 订单状态：0申请 1成功 2失败
		RechargeRecord r = new RechargeRecord(p2_ordernumber, userid, p7_productcode, money, p3_money, start,createtime);
		int jg = rechargeRecordMapper.insert(r);
		return jg;
	}

	/**
	 * 修改订单状态
	 */
	public int upRecord_Play(String p2_ordernumber, int start) {
		RechargeRecord r = rShow(p2_ordernumber);
		r.setStart(start);
		int jg = rechargeRecordMapper.updateById(r);
		return jg;
	}

}
